Conditions | 1 |
Paths | 1 |
Total Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var blocktrail = require('../'), |
||
58 | describe('wallet', function() { |
||
59 | _.forEach(vectors.password_reset_case, function(f) { |
||
60 | it('should allow password RESET', function() { |
||
61 | var expectedSecret = new Buffer(f.expectedSecret, 'hex'); |
||
62 | |||
63 | // user lost passphrase, has backup sheet |
||
64 | var recoverySecret = new Buffer(f.recoverySecret,'hex'); |
||
65 | // ^ this comes from Blocktrail |
||
66 | |||
67 | var recoveryEncryptedSecret = f.recoveryEncryptedMnemonic; |
||
68 | // ^ the user keeps this |
||
69 | |||
70 | var decodedRS = V3Crypt.EncryptionMnemonic.decode(recoveryEncryptedSecret); |
||
71 | var decryptedSecret = V3Crypt.Encryption.decrypt(decodedRS, recoverySecret); |
||
72 | assert.equal(decryptedSecret.toString('hex'), expectedSecret.toString('hex')); |
||
73 | }); |
||
74 | }); |
||
75 | |||
76 | |||
77 | it('uses secret to decrypt primarySeed', function() { |
||
78 | _.forEach(vectors.decryptonly, function(vector, key) { |
||
79 | it ('vector ' + key + ' should decrypt and produce the same checksum', function() { |
||
80 | var passphrase = new Buffer(vector.password, 'hex'); |
||
81 | var encryptedSecretMnemonic = vector.encryptedSecret; |
||
82 | var primaryEncryptedSeedMnemonic = vector.primaryEncryptedSeed; |
||
83 | |||
84 | var decodedSecret = V3Crypt.EncryptionMnemonic.decode(encryptedSecretMnemonic); |
||
85 | var decryptedSecret = V3Crypt.Encryption.decrypt(decodedSecret, passphrase); |
||
86 | |||
87 | var decodedPrimarySeed = V3Crypt.EncryptionMnemonic.decode(primaryEncryptedSeedMnemonic); |
||
88 | var decryptedPrimarySeed = V3Crypt.Encryption.decrypt(decodedPrimarySeed, decryptedSecret); |
||
89 | |||
90 | var node = bitcoin.HDNode.fromSeedBuffer(decryptedPrimarySeed); |
||
91 | assert.equal(node.getAddress(), vector.checksum); |
||
92 | }); |
||
93 | }); |
||
94 | }); |
||
95 | |||
96 | it('encryption should produce valid encryption of the wallet seed', function() { |
||
97 | this.timeout(0); |
||
98 | var passphrase = new Buffer('S2SZKBjdLwfnpesqEw9DNbaCvM2X8s9GmBcKfqBkrHtNYA8XQ5nfhzDgnT5aq5HedEYXhn3nbtpukzxaGgB2cxxBCkJJdBQJ'); |
||
99 | var primarySeed = randomBytes(Wallet.WALLET_ENTROPY_BITS / 8); |
||
100 | |||
101 | var secret = randomBytes(Wallet.WALLET_ENTROPY_BITS / 8); |
||
102 | var encryptedSecret = V3Crypt.Encryption.encrypt(secret, passphrase); |
||
103 | assert.equal(secret.toString(), V3Crypt.Encryption.decrypt(encryptedSecret, passphrase).toString()); |
||
104 | |||
105 | var encryptedPrimarySeed = V3Crypt.Encryption.encrypt(primarySeed, passphrase); |
||
106 | assert.equal(primarySeed.toString(), V3Crypt.Encryption.decrypt(encryptedPrimarySeed, passphrase).toString()); |
||
107 | |||
108 | var recoverySecret = randomBytes(Wallet.WALLET_ENTROPY_BITS / 8); |
||
109 | var recoveryEncryptedSecret = V3Crypt.Encryption.encrypt(secret, recoverySecret); |
||
110 | assert.equal(secret.toString(), V3Crypt.Encryption.decrypt(recoveryEncryptedSecret, recoverySecret).toString()); |
||
111 | |||
112 | var backupInfo = { |
||
113 | encryptedPrimarySeed: V3Crypt.EncryptionMnemonic.encode(encryptedPrimarySeed), |
||
114 | encryptedSecret: V3Crypt.EncryptionMnemonic.encode(encryptedSecret), |
||
115 | recoveryEncryptedSecret: V3Crypt.EncryptionMnemonic.encode(recoveryEncryptedSecret) |
||
116 | }; |
||
117 | |||
118 | _.forEach(backupInfo, function(val, key) { |
||
119 | var cmp; |
||
120 | if (key === 'encryptedPrimarySeed') { |
||
121 | cmp = encryptedPrimarySeed; |
||
122 | } else if (key === 'encryptedSecret') { |
||
123 | cmp = encryptedSecret; |
||
124 | } else if (key === 'recoveryEncryptedSecret') { |
||
125 | cmp = recoveryEncryptedSecret; |
||
126 | } |
||
127 | |||
128 | assert.equal(cmp.toString(), V3Crypt.EncryptionMnemonic.decode(val).toString()); |
||
129 | }); |
||
130 | }); |
||
131 | }); |
||
132 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.